package com.sourcespro.mybatis.controller;

import com.sourcespro.mybatis.crudparams.swaggernotes.TreeNotes;
import com.sourcespro.mybatis.entity.Tree;
import com.sourcespro.mybatis.service.TreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类
 *
 * @author 张浩伟
 * @date 2018/07/09
 */
@Api(value = "Tree", description = "分类")
@RestController
@RequestMapping(value ="/Tree")
public class TreeController {

    private static final String OK = "ok";

    private static final Logger logger = LoggerFactory.getLogger(TreeController.class);

    @Autowired
    private TreeService service;

    @ApiOperation(value = "添加", notes = TreeNotes.ADD_NOTE)
    @ApiImplicitParam(name = "record", value = TreeNotes.ADD_VALUE, required = true, dataType = "Tree")
    @PostMapping("add")
    public String add(@RequestBody @Validated Tree record, BindingResult result){

        Tree saveResult = service.add(record);
        return OK;
    }

    @ApiOperation(value = "查询")
    @GetMapping("findByPid/{pid}")
    public List<Tree> findByPid(@PathVariable("pid") Long pid){

        return service.findByPid(pid);
    }

    @ApiOperation(value = "查询")
    @GetMapping("findByPidCascaded/{pid}")
    public List<Tree> findByPidCascaded(@PathVariable("pid") Long pid){
        return service.findByPidCascaded(pid);
    }


    @ApiOperation(value = "删除", notes = "")
    @GetMapping("delete/{id}")
    public String edit(@PathVariable("id") Long id){

        int delete = service.delete(id);

        return OK;
    }

}